<template>
  <div>
    <!-- 商品详情页 -->
    <!-- 轮播图 -->
    <div class="pic" v-for="(item, index) in detail" :key="index">
      <van-swipe>
        <van-swipe-item v-for="item1 in item.imgURL" :key="item1">
          <img class="goods-img" :src="item1" alt="" />
        </van-swipe-item>

        <template #indicator="{ active }">
          <div class="custom-indicator">{{ active + 1 }}/5</div>
        </template>
      </van-swipe>
      <!-- 返回按钮和分享按钮 -->
      <van-nav-bar left-arrow @click-left="returnPage">
        <template #right>
          <van-icon name="share-o" size="18" @click="showShare = true" />
        </template>
      </van-nav-bar>
      <!-- 分享框 -->
      <van-share-sheet
        v-model:show="showShare"
        title="立即分享给好友"
        :options="options"
        @select="onSelect"
      />
      <!-- 商品标题 -->
      <!-- <div class="title" v-for="item in detail" :key="item"> -->
      <div class="title">
        <span class="price">￥{{ item.currentPrice }}</span>
        <span class="price-dis">￥{{ item.originalPrice }}</span>
        <span class="price-discount">券后价￥{{ item.discounts }}起</span>
        <!-- 8.8折券 -->
        <van-cell value="优惠" is-link>
          <!-- 使用 title 插槽来自定义标题 -->
          <template #title>
            <span class="custom-title discount-p">8.8折券</span>
          </template>
        </van-cell>
        <p class="title-main">{{ item.productName }}</p>
        <p class="title-small">{{ item.title }}</p>
      </div>
    </div>
  </div>
  <!-- 加入购物车 -->
  <van-action-bar>
    <van-action-bar-icon icon="chat-o" text="客服" dot />
    <van-action-bar-icon
      icon="cart-o"
      text="购物车"
      :badge="this.cartList.length"
      @click="cart"
    />
    <van-action-bar-button type="warning" text="加入购物车" @click="addCart" />
  </van-action-bar>
</template>

<script>
import { postUpdateCartListApi } from "@/utils/api";
import { mapState, mapActions, mapMutations } from "vuex";
import { ref } from "vue";
import { Toast } from "vant";
export default {
  setup() {
    const showShare = ref(false);
    const options = [
      { name: "微信", icon: "wechat" },
      { name: "微博", icon: "weibo" },
      { name: "复制链接", icon: "link" },
      { name: "分享海报", icon: "poster" },
      { name: "二维码", icon: "qrcode" },
    ];

    const onSelect = (option) => {
      Toast(option.name);
      showShare.value = false;
    };
    return {
      options,
      onSelect,
      showShare,
    };
  },
  async created() {
    await this.getDetailActions({
      // 根据路由id向数据库请求id对应商品信息数据
      id: this.$route.params.productId,
    });
  },
  data() {
    return {};
  },
  computed: {
    //购物车精简列表
    ...mapState("cart", ["cartList"]),
    ...mapState("store", ["detail"]),
  },

  methods: {
    async addCart() {
      // console.log(this.detail);
      //判断商品是否在购物车存在
      let flag = false;
      let arr = this.cartList.filter((val) => {
        if (val.productId === this.detail[0].productId) {
          flag = true;
          // console.log(1);
          return {
            productId: val.productId,
            productNum: ++val.productNum,
          };
        } else {
          return val;
        }
      });
      if (!flag) {
        arr.push({
          productId: this.detail[0].productId,
          productNum: 1,
        });
      }
      // 更新cartList
      this.updateCartList({ list: arr });
      //更新数据库
      await postUpdateCartListApi({
        uid: localStorage.getItem("userid"),
        carr: this.cartList,
      });
    },
    ...mapActions("store", ["getDetailActions"]),
    ...mapMutations("cart", ["updateCartList"]),
    // 顶部返回
    returnPage() {
      this.$router.go(-1);
    },
    // 跳转购物车
    cart() {
      this.$router.push("/cart");
    },
  },
};
</script>
<style lang="less">
.custom-indicator {
  position: absolute;
  right: 5px;
  bottom: 5px;
  padding: 2px 5px;
  font-size: 12px;
  background: rgba(57, 169, 237, 0.1);
}
.van-swipe {
  height: 290px !important;
}
.van-swipe-item {
  background-color: #39a9ed;
  text-align: center;
  line-height: 290px;
  height: 290px;
  img {
    width: 100%;
    height: 290px !important;
  }
}
.pic {
  position: relative;
  background: #fff;
  height: 800px;
  .van-nav-bar {
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 9999;
    background: none;
    .van-icon {
      color: #ffffff;
      font-size: 30px !important;
    }
  }
}
.custom-indicator {
  color: #fff;
}
.van-hairline--bottom::after {
  border: none;
}
.title {
  background: #fff;
  padding: 10px;
  height: 30px;
  font-size: 13px;
  .price {
    font-size: 23px;
    color: #fd3057;
  }
  .price-dis {
    color: #afafaf;
    text-decoration: line-through;
    margin: 0px 10px;
  }
  .price-discount {
    color: #fff;
    font-size: 16px;
    padding: 5px 10px;
    height: 30px;
    line-height: 30px;
    border-radius: 10px;
    text-align: center;
    background-color: #fd3057;
  }
}

.discount-p {
  background-color: #fdf0ef;
  color: #fd3057;
  padding: 5px 10px;
}
.van-cell {
  background-color: #fff;
}
.van-cell__value {
  color: #fd3057;
}
.van-cell__right-icon {
  color: #fd3057;
}
.title-main {
  font-size: 20px;
  color: #000;
  font-weight: bolder;
  margin-bottom: 10px;
}
.title-small {
  color: #b2b2b2;
  font-style: 13px;
}
</style>
